var app = angular.module('JiangGanApp')
app.controller('RegionMapCtrl', ['$scope','$state','DiagramService','$rootScope','$anchorScroll','$location', 'DataService', 'LeafletService',RegionMapCtrl]);


function RegionMapCtrl($scope,$state,DiagramService,$rootScope,$anchorScroll,$location,DataService,LeafletService){

	$rootScope.showMap = true;

	$scope.goBack = function () {
		$state.go('app.region.main');
	}

	$scope.selectedType = 'total_score';
	$scope.types = {
		'总分': 'total_score', 
		'警情': 'jq_score', 
		'案件': 'dfk_score', 
		'流口': 'flow_score', 
		'触网人数': 'netbar_hotel_score', 
		'黄赌毒': 'hdd_score',
		'边缘性场所': 'enter_score'
	};

	$scope.barConfig = {
    theme:'default',
    dataLoaded:true
	};
	$scope.$watch('selectedType', function (newv, oldv) {
		generateCharts(newv);
	});

	$scope.communities = {};
	$scope.originData = [];
	DataService.restGetApi('area_totals?filter={"where": {"date":"2017-08-15"}, "order":"total_score desc"}')
	.then(function success(data) {
		$scope.originData = data;
		generateCharts('total_score');
	});

	function generateCharts(newv) {
		$scope.communities = {};
		var dataMap = {
			xAxis:['0-5','6-11','12-17','18-23','24-30'],
			legend:['社区数量'],
			series:[
				{
					name: '社区数量',
					data: [0, 0, 0, 0, 0]
				}
			]
		}
		var maxValue = 30;
		if(newv == 'total_score') {
			maxValue = 75;
			dataMap.xAxis = ['0-14', '15-29', '30-44', '45-59', '60-75']
		}
		for(var i = 0; i < $scope.originData.length; i++) {
			$scope.communities[$scope.originData[i].community] = $scope.originData[i][newv];
			var percent = Math.floor($scope.originData[i][newv] / maxValue * 5);
			dataMap.series[0].data[percent] ++;
		}
		// $scope.barOption = DiagramService.drawLinearBar(dataMap, '');
		$scope.barOption = DiagramService.getStackBar('', dataMap.legend, dataMap.xAxis, [dataMap.series[0].data]);
		LeafletService.clearLayers();
		LeafletService.drawStations($scope.communities, maxValue);
	}


}
